<template>
	<div id="registerAgent">
		<div id="header">
			<img @click="$router.go(-1)" src="../../static/img/return.png"/>
			<p>商家注册</p>
		</div>
		<img id="logo" src="../../static/img/logo-01.png"/>
		<div class="register_item">
			<input type="text" v-model="phone" placeholder="请输入手机号" />
		</div>
		<div class="verification">
			<input type="text" v-model="code" placeholder="请输入验证码" />
			<!--<input v-if="verificationShow" @click="phoneCode" type="button" value="获取验证码" />
			<input v-if="!verificationShow" type="button" style="width: 10rem;" v-model="getVerification" />-->
			<input  type="button" style="width: 10rem;"  :value="getVerification" @click="phoneCode"/>
		</div>
		<div class="register_item">
			<input type="password" v-model="password" placeholder="请输入密码" />
		</div>
		<div class="register_item">
			<input type="password" v-model="passwordAgain" placeholder="请再次输入密码" />
		</div>
		<div class="agreement">
			<input type="checkbox" v-model="ckecked" />
			<p>我已仔细阅读并同意接受<router-link to="/agreementAgent">《用户使用协议》</router-link></p>
		</div>
		<input id="sub" @click="register" type="button" value="注册" />
	</div>
</template>

<script>
export default{
	name: 'registerAgent',
	data:function(){
		return{
			phone: '',//手机号
			code: '',//验证码
			getVerification: '获取验证码',//获取验证码
			keyCode:'',//加密验证码
//			verificationShow: true,//获取验证码切换
			password: '',//密码
			passwordAgain: '',//重复密码
			invite: '',//邀请码
			time: 60,
			ckecked: false,//选择框
		}
	},
	methods:{
		register:function(){
			this.$loading();
			var _this = this;
			if(!/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(_this.phone)){
				_this.$alert("手机号格式错误");
				$("#loading").remove();
				return;
			}
			if(_this.code==''){
				_this.$alert("验证码不能为空");
				$("#loading").remove();
				return;
			}
			if(_this.password=='' || _this.passwordAgain==''){
				_this.$alert("密码不能为空");
				$("#loading").remove();
				return;
			}
			if(_this.password != _this.passwordAgain){
				_this.$alert("两次输入密码不一致");
				$("#loading").remove();
				return;
			}
			if(!_this.ckecked){
				_this.$alert("请先同意《用户试用协议》");
				$("#loading").remove();
				return;
			}
			let postData = _this.$qs.stringify({
				    phone:_this.phone,
				    code:_this.code,
				    keyCode:_this.keyCode,
				    invite:_this.invite,
				    password:_this.password,
				    source: '用户介绍',
			});
			_this.$axios.post('https://www.ganjinshi.com/gjs_service/agent/autho/public/signup',postData).then(function(res){
				console.log(res.data.message);
				if(res.data.message=="succ"){
					_this.$alert("注册成功！请在电脑浏览器输入https://www.ganjinshi.com或者搜索赶紧试登录。")
					$("#loading").remove();
				}else{
					_this.$alert(res.data.result);
					console.log(res.data.result);
					$("#loading").remove();
				}
			}).catch(function (error){
			    console.log(error);
				$("#loading").remove();
			});
		},
		//--------获取验证码倒计时-------
		countDown:function(){
			var _this = this;
			if(_this.time != 60){
				return;
			}
//			_this.verificationShow = false;
			_this.getVerification = '60s后重新获取';
			var Time = setInterval(function(){
				if(_this.time == 1){
//					_this.verificationShow = true;
					_this.time = 60;
					_this.getVerification = '获取验证码';
					clearTimeout(Time);
				}else{
					_this.time -- ;
					_this.getVerification = _this.time + 's后重新获取';
				}
			},1000);
			
		},
		phoneCode:function(){
			var _this = this;
			if(_this.time != 60){
				return;
			}
			var reg=11 && /^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\d{8}$/;
			if(!reg.test(this.phone)){
				this.$alert('请输入正确的手机号码');
				return;
			}
			_this.countDown();
			let postData = _this.$qs.stringify({
				phone:_this.phone,
			});
			_this.$axios.post('/sms/public/phoneCode',postData).then(function(res){
				if(res.data.message=="succ"){
					_this.keyCode = res.data.result;
					_this.$alert("发送成功");
				}else{
					_this.$alert("发送失败");
					console.log(res.data.result);
				}   
			}).catch(function (error) {
			    console.log(error);
			});
		},
	},
	created: function(){
		if(this.$route.query.invite){
			this.invite = this.$route.query.invite;
		}
	}
}
</script>

<style scoped>
/* ========== */
/* = Header = */
/* ========== */
#header{
	width: 100%;
	background-color: #fff;
	position: fixed;
	top: 0;
	left: 0;
	height: 4rem;
	display: flex;
	align-items: center;
	justify-content: center;
	border-bottom: 1px solid #EEEEEE;
	font-size: 1.6rem;
	color: #4F4F4F;
	box-sizing: border-box;
	z-index: 10;
}
#header img{
	height: 2rem;
	display: block;
	position: absolute;
	top: 1rem;
	left: 2rem;
}
#registerAgent{
	min-height: 100vh;
	/*height: 100%;*/
	padding-top: 5rem;
	box-sizing: border-box;
	background-color: #fff;
}
#logo{
	width: 40%;
	display: block;
	margin: 4rem auto;
}
.register_item{
	width: 90%;
	margin: 0 auto;
	display: flex;
	align-items: center;
	border-bottom: 1px solid #EEEEEE;
	height: 4rem;
	padding: 0 1rem;
	box-sizing: border-box;
}
/*.register_item img{
	height: 1.4rem;
	display: block;
	margin-right: 1rem;
}*/
.register_item input{
	width: 100%;
	border: none;
	height: 100%;
	font-size: 1.2rem;
}
.verification{
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 0 1rem;
	border-bottom: 1px solid #EEEEEE;
	height: 4rem;
	width: 90%;
	margin: 0 auto;
	box-sizing: border-box;
}
.verification input[type='text']{
	width: calc(100% - 8rem);
	border: none;
	height: 100%;
	font-size: 1.2rem;
}
.verification input[type='button']{
	height: 2.2rem;
	border: 1px solid #FF6565;
	border-radius: 50px;
	box-sizing: border-box;
	width: 8rem;
	color: #FF6565;
	background-color: #fff;
}
#sub{
	width: 80%;
	margin: 0 auto;
	height: 4rem;
	border-radius: 50px;
	background-color: #FF6565;
	color: #FFFFFF;
	font-size: 1.4rem;
	border: none;
	display: block;
	margin: 0 auto;
	margin-top: 3rem;
}
.agreement{
	width: 90%;
	margin: 0 auto;
	display: flex;
	align-items: center;
	color: #787878;
	font-size: 1.2rem;
	margin-top: 1.5rem;
}
.agreement a{
	color: #FF6565;
}
input[type='checkbox']{
	-webkit-appearance: none; 
	-moz-appearance: none;
	appearance: none;
	width: 1.4rem;
	height: 1.4rem;
	border: 1px solid #cccccc;
	background-color: #FFFFFF;
	outline: none;
	margin-right: 0.5rem;
}
input[type='checkbox']:checked{
	background: url('../../static/img/checked.png') no-repeat center;
	background-size: 90% auto;
}
</style>
